﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.Json;


namespace Minecraft_of_WebKit2
{
    public class server
    {
        public static void runHttpServer()
        {
            string url = "http://localhost:8080/";
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add(url);

            try
            {
                listener.Start();
                Console.WriteLine("Listening for requests on " + url);

                while (true)
                {
                    HttpListenerContext context = listener.GetContext();
                    HttpListenerRequest request = context.Request;
                    HttpListenerResponse response = context.Response;

                    if (request.HttpMethod == "POST")
                    {
                        // 读取POST请求中的JSON数据
                        using (Stream body = request.InputStream)
                        {
                            using (StreamReader reader = new StreamReader(body, Encoding.UTF8))
                            {
                                string requestBody = reader.ReadToEnd();
                                // 解析JSON数据
                                var jsonDoc = JsonDocument.Parse(requestBody);
                                if (jsonDoc.RootElement.TryGetProperty("target", out var winName) && jsonDoc.RootElement.TryGetProperty("base64", out var base64))
                                {
                                    string responseString = "OK";
                                    byte[] buffer = Encoding.UTF8.GetBytes(responseString);
                                    response.ContentType = "text/html";
                                    response.ContentLength64 = buffer.Length;
                                    Stream output = response.OutputStream;
                                    output.Write(buffer, 0, buffer.Length);
                                    output.Close();

                                    try
                                    {
                                        Rendering.run(winName.GetString(), base64.GetString());
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine(e);
                                    }
                                }
                                else
                                {
                                    string responseString = "ERROR";
                                    byte[] buffer = Encoding.UTF8.GetBytes(responseString);

                                    response.ContentType = "text/html";
                                    response.ContentLength64 = buffer.Length;
                                    response.StatusCode = (int)HttpStatusCode.BadRequest;
                                    Stream output = response.OutputStream;
                                    output.Write(buffer, 0, buffer.Length);
                                    output.Close();
                                }
                            }
                        }
                    }
                    else
                    {
                        // 处理其他类型的请求，如GET
                        string responseString = "Hello, Minecraft_of_WebKit2!";
                        byte[] buffer = Encoding.UTF8.GetBytes(responseString);

                        response.ContentType = "text/html";
                        response.ContentLength64 = buffer.Length;
                        Stream output = response.OutputStream;
                        output.Write(buffer, 0, buffer.Length);
                        output.Close();
                    }
                }
            }
            catch (HttpListenerException ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                listener.Stop();
            }
        }
    }
}